﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using System.Collections;
using PRScreenWithKinect.Model;

namespace PRScreenWithKinect
{
	/// <summary>
	/// Interaction logic for AboutFac.xaml
	/// </summary>
    public partial class AboutFac : UserControl, UserControlInterfaceForGesture
	{
            enum Catagory
        {
            History=1,Cirriculum=2,Activity=3,Personel=4,
        }
        private ArrayList controllerElement;
        private ArrayList dataControllerElement;
        private Grid prevMenu;
        private UIElement hitElement = null;
        private UIElement hittedElement = null;
        private Double focusOpacity = 0.7, defaultOpacity = 0.3;
        private SolidColorBrush focusColor = Brushes.Orange, defaultColor = Brushes.White;
        private int pageIndex = 0;
        private ListBox[] ar;
        private int mainController;

		public AboutFac()
		{
			this.InitializeComponent();

            // Add Menu to controllerElement ;s
            controllerElement = new ArrayList();
            controllerElement.Add(Menu1);
            controllerElement.Add(Menu2);
            controllerElement.Add(Menu3);
            controllerElement.Add(Menu4);
         
            mainController = controllerElement.Count;


            // Set Hit Element = menu1 ;
            hittedElement = Menu1;

            /// Create DataElementcontroller 
            dataControllerElement = new ArrayList();


            /// Load Data with Catagory All 
            loadDataFromJSON(Catagory.History);
		}
        private void createPageBullet(int n)
        {
            pageBulletStack.Children.RemoveRange(0, pageBulletStack.Children.Count);
            for (int i = 0; i < n; i++)
            {
                Ellipse myEllipse = new Ellipse();

                // Create a SolidColorBrush with a red color to fill the 
                // Ellipse with.

                myEllipse.Fill = Brushes.Gray;
                if (i == 0)
                {
                    myEllipse.Opacity = focusOpacity;
                }
                else
                {
                    myEllipse.Opacity = defaultOpacity;
                }



                // Set the width and height of the Ellipse.
                myEllipse.Width = 20;
                myEllipse.Height = 20;
                myEllipse.Margin = new Thickness(5, 0, 0, 0);
                //   Console.WriteLine("Add Bullet");
                pageBulletStack.Children.Add(myEllipse);
            }


        }
      
        private void loadDataFromJSON(Catagory c){
            //// Remove all data from controllerElement 
            if (prevMenu != null)
            {
                ((Path)prevMenu.Children[0]).Fill = defaultColor;
                viewer.Children.RemoveRange(0, viewer.Children.Count);
               
                //controllerElement.RemoveRange(mainController, controllerElement.Count - mainController);
            }
            if (c == Catagory.History)
            {
                prevMenu = Menu1;

            }
            else if (c == Catagory.Cirriculum)
            {
                prevMenu = Menu2;
            }
            else if (c == Catagory.Activity)
            {
                prevMenu = Menu3;
            }
            else if (c == Catagory.Personel)
            {
                prevMenu = Menu4;
            }

            ((Path)prevMenu.Children[0]).Fill = focusColor;



            ///// Load Data from Json

            //// Image (W,H) = 468,351 
            int wImageView = 460;
            int hImageView = 345;
            Thickness b = new Thickness(0, 3, 4, 3);
          //  Console.WriteLine("About Fac catagory : " + (int)c);
            AboutFacModel[] aboutFacModel = DataFactory.Instance.getAboutFacByCatagory((int)c).ToArray();
            for (int i = 0; i < aboutFacModel.Length; i++)
            {
                Image t = new Image();
                t.Height = hImageView;
                t.Width = wImageView;
                t.Source = new BitmapImage(new Uri(aboutFacModel[i].path));
                //t.Background = Brushes.Cyan;
                t.Margin =b;
                viewer.Children.Add(t);
            }

            /// Create Page Bullet with parameter count_page
            createPageBullet(aboutFacModel.Length);
        
        }

        public void finishHold()
        {
            if (hitElement.Equals(Menu1))
            {
                loadDataFromJSON(Catagory.History);
            }
            else if (hitElement.Equals(Menu2))
            {
                loadDataFromJSON(Catagory.Cirriculum);
            }
            else if (hitElement.Equals(Menu3))
            {
                loadDataFromJSON(Catagory.Activity);
            }
            else if (hitElement.Equals(Menu4))
            {
                loadDataFromJSON(Catagory.Personel);
            }
       
           // Console.WriteLine("Type Hit Element : " + hitElement.GetType());
            hittedElement = hitElement;
            hitElement = null;
        }

        public void swipeLeft()
        {
            doSwipe(Swipe.Left);
        }

        public void swipeRight()
        {
            doSwipe(Swipe.Right);
        }


        public void doSwipe(Swipe s)
        {
            if (s == Swipe.Left && pageIndex < (viewer.Children.Count - 1))
            {



                pageIndex++;
                pageBulletStack.Children[pageIndex].Opacity = focusOpacity;
                pageBulletStack.Children[pageIndex - 1].Opacity = defaultOpacity;

            }
            else if (s == Swipe.Right && pageIndex > 0)
            {

                pageIndex--;
                pageBulletStack.Children[pageIndex].Opacity = focusOpacity;
                pageBulletStack.Children[pageIndex + 1].Opacity = defaultOpacity;

            }
            scrollView.ScrollToPosition(scrollView.Width * pageIndex, 0);

         //   Console.WriteLine("News(doSwipe) : " + pageIndex);
        }


        public void zoomContent(int i)
        {
           // throw new NotImplementedException();
        }

        public void closeZoom()
        {
           // throw new NotImplementedException();
        }

        public bool hitTest(Point p, Window w)
        {
            Boolean isHit = false;

            foreach (UIElement c in controllerElement)
            {
                Point RhandRelativePoint = c.TransformToAncestor(w).Transform(new Point(0, 0));
                Point delta = new Point(p.X - RhandRelativePoint.X, p.Y - RhandRelativePoint.Y);
                if (VisualTreeHelper.HitTest(c, delta) != null)
                {
                    if (c.Equals(hitElement) && !c.Equals(hittedElement))
                    {

                        return true;

                    }
                    else
                    {

                        hitElement = c;
                        return false;

                    }
                }
            }




            return isHit;
        }
    }
}